package khleuven.mondzorg.db;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.test.InstrumentationTestCase;
import android.util.Log;

import khleuven.mondzorg.db.readers.EntryBuilder;
import khleuven.mondzorg.db.readers.EntryBuilderFactory;
import khleuven.mondzorg.db.readers.EntryBuilderType;
import khleuven.mondzorg.domain.Definition;
import khleuven.mondzorg.domain.DentalCareApp;
import khleuven.mondzorg.domain.DomainException;
import khleuven.mondzorg.domain.Entry;

public class EntryBuilderTest extends InstrumentationTestCase {

	EntryBuilder test;
	DentalCareApp memory = DentalCareApp.getInstance();
	private int original_size;
	private Entry first;
	//private Entry last;
	
	protected void setUp() throws Exception {
		super.setUp();
		
		ArrayList<Entry> all = memory.getAllEntries();
		original_size = all.size();
		Log.v("MEREL","aantal voor laden: "+original_size);
		if(original_size > 0) {
			first = all.get(0);
			//last = all.get(original_size-1);
		}
	}

	/**
	 * TODO Writing of test still in process...
	 * 
	 * The main goal is to check that no exceptions occur and that the DentalCareApp holds at least one
	 * Definition more then before the test.
	 * 
	 * @throws ClientProtocolException
	 * @throws IOException
	 * @throws JSONException
	 * @throws DBException 
	 * @throws DomainException 
	 */
	@SuppressWarnings("deprecation")
	public void testHardCodedBuilder_definition_equals_compareTo_is_correctly_implemented() throws DBException, DomainException {
		EntryBuilderFactory.createEntryBuilderAndLoad(EntryBuilderType.HARDCODED);
		
		ArrayList<Entry> updatedDefinitions = memory.getAllEntries();
		assertTrue("There should be at least one entry loaded.", updatedDefinitions.size() > 1);
		
		Collections.sort(updatedDefinitions); // make sure the Definitions are first
		assertTrue("Sorting Entries should have Definitions first", updatedDefinitions.get(0) instanceof Definition);
		
		first = new Definition(updatedDefinitions.get(0).getLanguage(),
				updatedDefinitions.get(0).getDestination(),
				((Definition)updatedDefinitions.get(0)).getDefinition(),
				((Definition)updatedDefinitions.get(0)).getExplanation());
	
		assertTrue("Equals of definitions is working", updatedDefinitions.get(0).equals(first));
		assertTrue("CompareTo of definitions is working", updatedDefinitions.get(0).compareTo(first) == 0);
		
		assertTrue("Removing new object with equals == true should work", updatedDefinitions.remove(first));
	}
}
